### version 0.9 formal edition

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome(executable_path='D:/chromedriver.exe')
driver.set_window_size(1280,720)

# 本脚本为非登录操作，登录后操作可能会获取硬币数失败
uid = input('请输入up的uid：')
url = 'https://space.bilibili.com/' + uid + '/video'
driver.get(url)

# # 获取up主的昵称
# nickname = driver.find_element(By.XPATH, '//*[@id="h-name"]').text
# print("Up主的昵称为:", nickname)

# 定义一些参数，生成csv表头
i = 1
title = ''
play = 0
like = 0
coin = 0
collect = 0
share = 0
commet = 0
# coding = utf-8
filename = 'test3.csv'
with open(filename, 'w', encoding='utf-8-sig') as file_object:
    file_object.write('标题,播放量,点赞,硬币,收藏,分享,弹幕,评论\n')

# 统计视频投稿的页数
sleep(3)
xpage = driver.find_element(By.XPATH, '//*[@id="submit-video-list"]/ul[3]/span[1]').text
xpage = xpage.replace('共 ','')
xpage = xpage.replace(' 页，','')
print('本up主有:',xpage,'页投稿')

# 投稿页数循环操作
for x in range(int(xpage)):

    # 投稿主页循环操作
    for i in range(1,30):
        sleep(3)
        print(i)
        driver.find_element(By.XPATH,'//*[@id="submit-video-list"]/ul[2]/li['+str(i)+']/a[2]').click()
        driver.switch_to.window(driver.window_handles[1])

        # 已进入子网页
        sleep(5)

        # 获取标题数据
        title1 = driver.find_element(By.XPATH,'//*[@id="viewbox_report"]/h1').text
        title1 = title1.replace('活动作品', '')
        print('获取标题数据', title1)

        # 获取播放量数据
        play1 = driver.find_element(By.XPATH,'//*[@id="viewbox_report"]/div/span[1]').get_attribute('title')
        play1 = play1.replace('总播放数', '')
        print('获取播放量', play1)

        # 获取点赞数
        like1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[1]').get_attribute('title')
        like1 = like1.replace('点赞数', '')
        print('获取点赞数', like1)

        # 获取投币数
        coin1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[2]').text
        coin1 = coin1.replace('投币', '0')
        coin1 = coin1.replace('投硬币枚数', '')
        print('获取投币数', coin1)

        # 获取收藏数
        collect1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[3]').text
        collect1 = collect1.replace('收藏人数', '')
        collect1 = collect1.replace('收藏', '0')
        print('获取收藏数', collect1)

        # 获取分享数
        share1 = driver.find_element(By.XPATH,'//*[@id="arc_toolbar_report"]/div[1]/span[4]').text
        share1 = share1.replace('分享', '0')
        print('获取分享数', share1)

        # 获取弹幕数
        danmuku1 = driver.find_element(By.XPATH, '//*[@id="viewbox_report"]/div/span[2]').text
        danmuku1 = danmuku1.replace('总弹幕数', '')
        print('获取弹幕数', danmuku1)

        # 获取评论数
        driver.execute_script('window.scrollBy(0,1000)')
        sleep(2)
        comment1 = driver.find_element(By.CSS_SELECTOR, '#comment > div > div.b-head > span.b-head-t.results').text
        print('获取评论数', comment1)

        # 写入 csv 文件
        # coding=utf-8
        filename = 'test3.csv'
        with open(filename, 'a', encoding='utf-8-sig') as file_object:
            file_object.write(title1 +','+ play1 +','+ like1 +','+ coin1 +','+ collect1 +','+ share1 +','+ danmuku1 +','+ comment1 +'\n')
        print("csvfile write success")
        sleep(5)

        # 关闭子网页，退回到投稿主页
        driver.close()
        driver.switch_to.window(driver.window_handles[0])

    # 点击下一页
    driver.find_element(By.CSS_SELECTOR, '#submit-video-list > ul.be-pager > li.be-pager-next').click()

print('数据已经统计完成，20秒后退出')
sleep(20)
driver.quit()